package controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import entity.Essay;
import entity.Notice;
import service.BlogService;
import service.NoticeService;

@RequestMapping("console")
@Controller
public class Console {

	@Resource(name = "blogServiceImpl")
	private BlogService consoleBlogServiceImpl;
	
	@Resource(name="noticeServiceImpl")
	private NoticeService noticeService;

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public BlogService getConsoleBlogServiceImpl() {
		return consoleBlogServiceImpl;
	}

	public void setConsoleBlogServiceImpl(BlogService consoleBlogServiceImpl) {
		this.consoleBlogServiceImpl = consoleBlogServiceImpl;
	}

	@RequestMapping("console")
	public String consoleReq() {
		return "console/console";
	}

	@RequestMapping("console_blog_list")
	public ModelAndView consoleBlogList(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("console/console_blog_list");
		List<Essay> essays = consoleBlogServiceImpl.getAll();
		mv.addObject("essays", essays);
		return mv;
	}

	@RequestMapping("console_blog_add")
	public String consoleBlogAdd() {
		return "console/console_blog_add";
	}

	@RequestMapping("console_blog_add_save")
	public String consoleBlogAddSave(HttpServletRequest request) {

		String strId = request.getParameter("id");
		String title = request.getParameter("essay_title");
		String content = request.getParameter("add_essays");
		if (strId.equals("")) {
			consoleBlogServiceImpl.addNewEssay(title, content);
		} else {
			int id = Integer.parseInt(strId);
			consoleBlogServiceImpl.updateById(id, title, content);
		}
		return "redirect:console_blog_list";
	}

	@RequestMapping("console_blog_update")
	public ModelAndView consoleBlogUpdate(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("console/console_blog_add");
		int id = Integer.parseInt(request.getParameter("id"));
		Essay essay = consoleBlogServiceImpl.getById(id);
		mv.addObject("essay", essay);
		return mv;
	}

	@RequestMapping("console_blog_delete")
	public String consoleBlogDelete(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		consoleBlogServiceImpl.delEssayById(id);
		return "redirect:console_blog_list";
	}

	@RequestMapping("console_blog_open")
	public String consoleBlogOpen(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		consoleBlogServiceImpl.openEssay(id);
		return "redirect:console_blog_list";
	}

	@RequestMapping("console_blog_close")
	public String consoleBlogClose(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		consoleBlogServiceImpl.closeEssay(id);
		return "redirect:console_blog_list";
	}
	
	@RequestMapping("console_notice")
	public ModelAndView consoleNotice(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("console/console_notice"); 
		Notice notice = noticeService.getNotice();
		mv.addObject("notice", notice);
		return mv;
	}
	
	@RequestMapping("console_notice_save")
	public String consoleNoticeSave(HttpServletRequest request){
		int id=Integer.parseInt(request.getParameter("id"));
		String title = request.getParameter("notice_title");
		String content =request.getParameter("notice_content");
		Notice notice = new Notice();
		notice.setId(id);
		notice.setContent(content);
		notice.setTitle(title);
		noticeService.updateNotice(notice);
		return "redirect:console";
	}

	@RequestMapping("console_blog_add_essays")
	public String consoleBlogAddEssays() {
		RandomEssay();
		return "redirect:console_blog_list";
	}

	private void RandomEssay() {
		String res = "古时候古代信息技术不发达所以人们从这一个地区到那一个地区传递信息都非常不方便于是他们将写好的诗编成歌而诗歌就从人们的口中传递诗歌起源于上古的社会生活因劳动生产两性相恋原始宗教等而产生的一种有韵律富有感情色彩的语言形式尚书·虞书记载诗言志歌咏言声依永律和声礼记·乐记记载诗言其志也歌咏其声也舞动其容也三者本于心然后乐器从之早期诗歌与乐舞是合为一体的诗即歌词在实际表演中总是配合音乐舞蹈而歌唱后来诗歌乐舞各自发展独立成体以入乐与否区分歌与诗入乐为歌不入乐为诗诗从歌中分化而来为语言艺术而歌则是一种历史久远的音乐文学诗经是入乐歌唱的严格地说它是歌正因为如此诗经被学者称之为我国音乐文学成熟的标志";
		List<Essay> list = new ArrayList<Essay>();
		Random r = new Random();

		for (int i = 0; i < 50; i++) {
			int b_index = r.nextInt(res.length());
			int e_index = r.nextInt(res.length());

			int frist = b_index < e_index ? b_index : e_index;
			int last = e_index > b_index ? e_index : b_index;

			int t_index = r.nextInt(res.length());
			int len = r.nextInt(15);
			int t_end = (t_index + len) > res.length() ? res.length() : t_index + len;

			Essay e = new Essay();
			e.setTitle(res.substring(t_index, t_end));
			e.setReading(r.nextInt(1000));
			e.setComment(r.nextInt(1000));
			e.setOpen(r.nextBoolean());
			e.setTime(new Timestamp(new Date().getTime()));
			e.setContent(res.substring(frist, last));
			list.add(e);
		}

		for (Essay ess : list) {
			consoleBlogServiceImpl.addNewEssay(ess);
		}
	}
}
